<link rel="stylesheet" type="text/css" href="/lib/zTree_v3/css/zTreeStyle/zTreeStyle.css">
<script type="text/javascript" src="/lib/zTree_v3/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/lib/zTree_v3/js/jquery.ztree.excheck.js"></script>

<!-- 该页面只负责生成初始的数据结构,不包含任何勾选内容 -->
<div class="layui-row layui-col-space10" style="position: relative;">
    <div class="layui-col-md6">
        <div class="layuimini-main">
            <div style="text-align: center;">
                <div class="layui-inline">
                    <ul id="tree" class="ztree" style="width:230px; overflow:auto;"></ul>
                </div>
            </div>
        </div>
    </div>
</div>


<script>



    /*1 发送请求,获取树结构数据*/
    shared.ajaxGet('/admin/tree/getPermissionTree', {}, function (res) {
        if (res) {
            generateTree(res)
        }
    })


    /*2 通过树结构数据,生成树*/
    function generateTree(res) {

        var setting = {
            check: {
                enable: true,
                chkboxType: { "Y": "ps", "N": "ps" },
                // chkStyle: "checkbox",//显示 checkbox 选择框，默认checkbox可选择值radio

            },
            view: {
                showIcon: false
            },
            data: {
                simpleData: {
                    enable: true,
                    // idKey: "id",
                    // pIdKey: "pid"
                },
                key: {
                    title: "", //
                    name: "name"
                }
            }
        };

        var zTree = $.fn.zTree.init($("#tree"), setting, res);

        var treeObj = $.fn.zTree.getZTreeObj("tree");
        /*以上,树结构初始化完毕*/


        /*以下,对该打钩的进行打钩操作*/


        for (let index = 0; index < tempList.length; index++) {
            const element = tempList[index];
            if (element.split("-").length == 1) {
                var dbcode = element.split("-")[0]

                var nodes = treeObj.getNodes();
                var ArrayNodes = treeObj.transformToArray(nodes);

                ArrayNodes.forEach(element_ => {
                    if (element_.id == dbcode) {
                        treeObj.checkNode(element_, true, true);
                        return false
                    }
                });
            }

            else if (element.split("-").length == 2) {
                var dbcode = element.split("-")[0]
                var field = element.split("-")[1]

                var nodes = treeObj.getNodes();
                var ArrayNodes = treeObj.transformToArray(nodes);
                ArrayNodes.forEach(element_ => {
                    if (element_.id == field && element_.pId == dbcode) {
                        treeObj.checkNode(element_, true, true);
                        return false
                    }
                });
            }

            else if (element.split("-").length == 3) {
                var dbcode = element.split("-")[0]
                var field = element.split("-")[1]
                var pd = element.split("-")[2]

                var nodes = treeObj.getNodes();
                var ArrayNodes = treeObj.transformToArray(nodes);
                ArrayNodes.forEach(element_ => {
                    if (element_.id == pd && element_.pId == field && treeObj.getNodeByTId(element_.parentTId).pId == dbcode) {
                        treeObj.checkNode(element_, true, true);
                        return false
                    }
                });
            }
        }
    }


</script>